package objectAnalyzer;

import java.lang.reflect.Array;
import java.util.ArrayList;

/**
 * Created by haiqiang on 17/10/3.
 */
public class ObjectAnalyzer {
    private ArrayList<Object> visited = new ArrayList<>();

    public String toString(Object obj) {
        if (obj == null) {
            return "null";
        }

        if (visited.contains(obj)) {
            return "...";
        }

        visited.add(obj);

        Class cl = obj.getClass();
        if (cl == String.class) {
            return (String) obj;
        }

        if (cl.isArray()) {
            String r = cl.getComponentType() + "[]{";
            for (int i = 0; i < Array.getLength(obj); i++) {
                if (i > 0) r += ",";
                Object val = Array.get(obj, i);
                if (cl.getComponentType().isPrimitive()) {
                    r = r + val;
                } else {
                    r = r + toString(val);
                }

            }
            return r + "}";
        }

//        String r = cl.getNam
        return "";

    }
}
